package com.techwells.learningkites.adapter;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.techwells.learningkites.R;
import com.techwells.learningkites.networkaccess.KitesRequestUrl;
import com.techwells.learningkites.networkservice.model.Favorite;
import com.techwells.learningkites.networkservice.model.FavoriteWord;
import com.techwells.learningkites.networkservice.model.Word;
import com.techwells.taco.widget.OnDeleteListioner;


public class MyWordListAdapter extends BaseAdapter {

	private Context mContext;
	private List<FavoriteWord> mlist = null;
	private OnDeleteListioner mOnDeleteListioner;
	private boolean delete = false;
	private SoundPool soundPoolEn,soundPoolUs;

	// private Button curDel_btn = null;
	// private UpdateDate mUpdateDate = null;

	public MyWordListAdapter(Context mContext, List<FavoriteWord> mlist) {
		this.mContext = mContext;
		this.mlist = mlist;
		this.soundPoolEn = new SoundPool(50, AudioManager.STREAM_MUSIC, 100);
		this.soundPoolUs = new SoundPool(50, AudioManager.STREAM_MUSIC, 100);
		initSoundPool();
	}
	
	private void initSoundPool() {
		for (int i = 0; i < mlist.size(); i++) {
			soundPoolEn.load(KitesRequestUrl.BASE_URL_IMG + mlist.get(i).voiceEn, 1);
			soundPoolUs.load(KitesRequestUrl.BASE_URL_IMG + mlist.get(i).voiceUs, 1);
		}
		
	}

	public void setDelete(boolean delete) {
		this.delete = delete;
	}

	public boolean isDelete() {
		return delete;
	}

	public void setOnDeleteListioner(OnDeleteListioner mOnDeleteListioner) {
		this.mOnDeleteListioner = mOnDeleteListioner;
	}

	public int getCount() {

		return mlist.size();
	}

	public Object getItem(int pos) {
		return mlist.get(pos);
	}

	public long getItemId(int pos) {
		return pos;
	}

	public View getView(final int pos, View convertView, ViewGroup p) {

		final ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_mywordslist, null);
			viewHolder = new ViewHolder();
			viewHolder.seq = (TextView) convertView.findViewById(R.id.seq);
			viewHolder.word = (TextView) convertView.findViewById(R.id.word);
			viewHolder.soundEn = (TextView) convertView.findViewById(R.id.sound_en);
			viewHolder.soundUs = (TextView) convertView.findViewById(R.id.sound_us);
			viewHolder.collins = (TextView) convertView.findViewById(R.id.collins);
    		viewHolder.explanation = (TextView) convertView.findViewById(R.id.explanation);
    		viewHolder.wordPlayEn = (LinearLayout) convertView.findViewById(R.id.play_sound_en);
    		viewHolder.wordPlayUs = (LinearLayout) convertView.findViewById(R.id.play_sound_us);
    		viewHolder.favoriteTime = (TextView) convertView.findViewById(R.id.favorite_time);
    		viewHolder.delete_action = (TextView) convertView.findViewById(R.id.delete_action);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		if (pos < 10) {
			viewHolder.seq.setText((pos+1) + "." + "\t");
		}else {
			viewHolder.seq.setText((pos+1) + ".");
		}
        
		viewHolder.word.setText(mlist.get(pos).word);
		viewHolder.soundEn.setText(mlist.get(pos).soundmarkEn);
		viewHolder.soundUs.setText(mlist.get(pos).soundmarkUs);
		viewHolder.collins.setText(mlist.get(pos).levels.get(0));
		viewHolder.explanation.setText(mlist.get(pos).explaination);
		viewHolder.favoriteTime.setText("收藏于" + mlist.get(pos).submitTime);
		
		viewHolder.wordPlayEn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				soundPoolEn.play(pos+1, 1, 1, 1, 0, 1);
			}
		});
		viewHolder.wordPlayUs.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				soundPoolUs.play(pos+1, 1, 1, 1, 0, 1);
			}
		});

		final OnClickListener mOnClickListener = new OnClickListener() {

			@Override
			public void onClick(View view) {
				if (mOnDeleteListioner != null)
					mOnDeleteListioner.onDelete(pos);

			}
		};
		viewHolder.delete_action.setOnClickListener(mOnClickListener);
		return convertView;
	}

	public static class ViewHolder {
		public TextView seq,word,soundEn,soundUs,collins,cet4,cet6,stud,explanation,favoriteTime;
		public TextView delete_action;
		LinearLayout wordPlayEn,wordPlayUs;

	}

}
